//
//  SegmentNavBar.m
//  QTZ
//
//  Created by ski on 2017/8/8.
//  Copyright © 2017年 ski. All rights reserved.
//

#import "SegmentNavBar.h"

#define ANIMATION_DURATION 0.2


@interface SegmentNavBar ()

@property (nonatomic, copy) ButtonActionBlock actionBlock;

@property (nonatomic, assign) NSInteger selectIndex;

@property (nonatomic, strong) UIButton *selectBtn;

@property (nonatomic, strong) NSMutableArray *btnArr;

@property (nonatomic, strong) UIView *border;

@property (nonatomic, strong) UIView *borderView;

@end

@implementation SegmentNavBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.selectIndex = 0;
    }
    return self;
}

- (NSMutableArray *)btnArr
{
    if (!_btnArr) {
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}

- (void)setTitleArr:(NSArray *)titleArr
{
    [self layoutSubviewsWithTitles:titleArr];
}

- (void)layoutSubviewsWithTitles:(NSArray *)titles
{
    CGFloat centerX = FULL_WIDTH / titles.count / 2;
    CGFloat padding = centerX * 2;
    CGFloat centerY = self.height / 2;
    
    for (int i = 0; i < titles.count; i++) {
        NSString *title = [titles safeObjectAtIndex:i];
        UIButton *titleBtn = [UIButton buttonWithType:0];
        [titleBtn setTitle:title forState:0];
        titleBtn.titleLabel.font = FONT(14);
        [titleBtn sizeToFit];
        titleBtn.width = FULL_WIDTH / titles.count;
        titleBtn.height = self.height;
        [titleBtn setTitleColor:QTZ_Gray73 forState:0];
        titleBtn.tag = 1993 + i;
        [titleBtn addTarget:self action:@selector(handleBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArr addSafeObject:titleBtn];
        
        titleBtn.centerX = centerX + padding * i;
        titleBtn.centerY = centerY;
        [self addSubview:titleBtn];
    }
    
    self.border = [[UIView alloc] init];
    self.border.height = 2;
    self.border.bottom = self.height;
    self.border.centerX = centerX;
    self.border.backgroundColor = Theme_Color;
    [self addSubview:self.border];
    
    if (!self.borderView) {
        self.borderView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 1, FULL_WIDTH, 1)];
        self.borderView.backgroundColor = QTZ_Gray247;
        [self addSubview:self.borderView];
    }
    
    [self selectedIndex:0];
    
}

- (void)handleBtn:(UIButton *)btn
{
    [self selectedIndex:btn.tag - 1993];
}

- (void)selectedIndex:(NSInteger)index
{
    [(UIButton *)[self.btnArr safeObjectAtIndex:self.selectIndex] setTitleColor:QTZ_Gray73 forState:0];
    
    self.selectIndex = index;
    [(UIButton *)[self.btnArr safeObjectAtIndex:self.selectIndex] setTitleColor:Theme_Color forState:0];
    
    [self scrollBorder];
    
    self.actionBlock(self.selectIndex);
    
}

- (void)didSelectedIndex:(ButtonActionBlock)index
{
    self.actionBlock = index;
    self.actionBlock(self.selectIndex);
}

- (void)scrollBorder
{
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        
        UIButton *btn = (UIButton *)[self.btnArr safeObjectAtIndex:self.selectIndex];
        
        self.border.width = self.width / self.btnArr.count;
        self.border.centerX = btn.centerX;
        
    }];
}

@end
